<template>
  <!-- 审核管理组件 -->
  <div>
    <!-- 同意/拒绝弹窗-->
    <el-dialog :title="dialogTitle+'审批'" :visible.sync="dialogReject" :before-close="handleClose" @close="form.comments=''" width="450px">
      <el-form label-position="right" :inline="true" ref="form" :model="form" v-if="dialogTitle=='同意'">
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="审批意见" prop="comments">
              <el-input
                  type="textarea"
                  :autosize="{ minRows: 3, maxRows: 5}"
                  v-model="form.comments"
                  placeholder="请输入内容"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <el-form label-position="right" :inline="true" :rules="rules" ref="form" :model="form" v-if="dialogTitle=='拒绝'">
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="审批意见" prop="comments">
              <el-input
                  type="textarea"
                  :autosize="{ minRows: 3, maxRows: 5}"
                  v-model="form.comments"
                  placeholder="请输入内容"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogReject = false">取 消</el-button>
        <el-button type="primary" @click="getStatus">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import checkoutQuery from "@/http/apis/modules/checkoutQuery";
export default {
  props:{
    // 审核标题
    dialogTitle: {
      type: String,
      default: ""
    },
    // 审核弹窗
    dialogReject: {
      type: Boolean,
      default: false
    },
    // 审核弹窗
    relateId: {
      type: String,
      default: ""
    }
  },
  data() {
    const isNull = (rules, value, callBack) => {
      if (!value) {
        callBack(new Error("不能为空"));
      } else {
        callBack();
      }
    };
    return {
      form: {
        comments: "",/*审批意见*/
      },
      rules: {
        comments: { validator: isNull, trigger: "blur" },
      },
    };
  },
  methods:{
    // 同意/拒绝审批
    async getStatus() {
      if (this.dialogTitle === '同意') {
        // if(this.form.comments == "") {
        //   this.form.comments = "同意";
        // }
        let res = await checkoutQuery.doApproval({
            comments: this.form.comments==''?'同意':this.form.comments,
            relateId: this.relateId,
            operatorId: this.$store.state.user.userId,
        });
        if (res.data.code === "SUCCESS") {
          this.$message({ message: res.data.msg==''?'提交成功':res.data.msg, type: "success" });
        } else if (res.data.code === "ERROR") {
          this.$message({ message: res.data.msg==''?'提交失败':res.data.msg, type: "error" });
        }
        this.$emit("approvalList");
      } else {
        this.$refs.form.validate(async (valid) => {
          if (valid) {
            let res = await checkoutQuery.doRejected({
              comments: this.form.comments,
              relateId: this.relateId,
              operatorId: this.$store.state.user.userId,
            });
            if (res.data.code === "SUCCESS") {
              this.$message({ message: res.data.msg==''?'提交成功':res.data.msg, type: "success" });
            }else if (res.data.code === "ERROR") {
              this.$message({ message: res.data.msg==''?'提交失败':res.data.msg, type: "error" });
            }
            this.$emit("approvalList");
          }else {
            return;
          }
        })
      }
    },
    handleClose() {
      this.$emit("upStatus");
    }
  },
  created(){
  }
};
</script>

<style lang="less" scoped>
/deep/.el-textarea__inner{width: 320px;}
</style>
